var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
  data: {
    orderId: 0,
    orderInfo: {},
    orderGoods: [],
    handleOption: {}
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      orderId: options.id
    });
    this.getOrderDetail();
  },
  getOrderDetail() {
    let that = this;
    let user_id = wx.getStorageSync('userId');
    let orderdata = { user_id: user_id,order_id:that.data.orderId};
    util.request(api.OrderDetail,orderdata).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          orderInfo: res.data.orderlist,
          orderGoods: res.data.goodslist,
        });
      }
    });
  },
  payTimer() {
    let that = this;
    let orderInfo = that.data.orderInfo;
    setInterval(() => {
      console.log(orderInfo);
      orderInfo.add_time -= 1;
      that.setData({
        orderInfo: orderInfo,
      });
    }, 1000);
  },
  payOrder() {
    debugger
    let that = this;
    let openId = wx.getStorageSync('openId');
    if(!openId){
      util.getOpenId();
      openId = wx.getStorageSync('openId');
    }
    util.request(api.PayPrepayId, {
      openId: openId,
      orderId: that.data.orderId || 15
    }).then(function (res) {
      if (res.errno === 0) {
        const payParam = res.data;
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            console.log(res)
          },
          'fail': function (res) {
            console.log(res)
          }
        });
      }else{
        util.showErrorToast(res.data);
      }
    });

  },
  cancelOrder:function(){
    let that = this;
    let user_id = wx.getStorageSync('userId');
    let orderdata = { user_id: user_id, order_id: that.data.orderId };
    util.request(api.orderCancel, orderdata).then(function (res) {
      if (res.errno === 0) {
         wx.redirectTo({
           url: '../orderDetail/orderDetail?id=' + that.data.orderId,
         })
      }
    });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})